Skip to contentSkip to conversation

video: hyperv: hyperv_fb: Use physical memory for fb on HyperV Gen 1 VMs.

PATCH Superseded Wei Hu 4 participants 5 messages Oct 22, 2019 – Nov 2, 2019

Subsystems (4)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Wei Hu

author maintainer
#0 lore raw

PATCH 1/1 video: hyperv: hyperv_fb: Use physical memory for fb on HyperV Gen 1 VMs.

On Hyper-V, Generation 1 VMs can directly use VM's physical memory for their framebuffers. This can improve the efficiency of framebuffer and overall performence for VM. The physical memory assigned to framebuffer must be contiguous. We use CMA allocator to get contiguouse physicial memory when the framebuffer size is greater than 4MB. For size under 4MB, we use alloc_pages to achieve this.

To enable framebuffer memory allocation from CMA, supply a kernel parameter to give enough space to CMA allocator at boot time. For example:

    cma=130m

This gives 130MB memory to CAM allocator that can be allocated to framebuffer. If this fails, we fall back to the old way of using mmio for framebuffer.

---
 drivers/video/fbdev/Kconfig     |   1 +
 drivers/video/fbdev/hyperv_fb.c | 179 +++++++++++++++++++++++++-------
 kernel/dma/contiguous.c         |   2 +
 3 files changed, 147 insertions(+), 35 deletions(-)
drivers/video/fbdev/Kconfig +1 -0
index aa9541bf964b..f534059461ee 100644
@@ -2215,6 +2215,7 @@ config FB_HYPERV
 	select FB_CFB_COPYAREA
 	select FB_CFB_IMAGEBLIT
 	select FB_DEFERRED_IO
+	select DMA_CMA
 	help
 	  This framebuffer driver supports Microsoft Hyper-V Synthetic Video.
 
drivers/video/fbdev/hyperv_fb.c +144 -35

270 lines not on this page · raw message

kernel/dma/contiguous.c +2 -0
index 69cfb4345388..4553f4cca80e 100644
@@ -197,6 +197,7 @@ struct page *dma_alloc_from_contiguous(struct device *dev, size_t count,
 
 	return cma_alloc(dev_get_cma_area(dev), count, align, no_warn);
 }
+EXPORT_SYMBOL(dma_alloc_from_contiguous);
 
 /**
  * dma_release_from_contiguous() - release allocated pages
@@ -213,6 +214,7 @@ bool dma_release_from_contiguous(struct device *dev, struct page *pages,
 {
 	return cma_release(dev_get_cma_area(dev), pages, count);
 }
+EXPORT_SYMBOL(dma_release_from_contiguous);
 
 /**
  * dma_alloc_contiguous() - allocate contiguous pages
Signature · 1 line
2.20.1
#1 lore raw

drivers/video/fbdev/Kconfig config FB_HYPERV · line 2218

Quoted 1 line
drivers/video/fbdev/Kconfig +1 -0
+	select DMA_CMA

Thіs needs to be

select DMA_CMA if HAVE_DMA_CONTIGUOUS

drivers/video/fbdev/hyperv_fb.c static void hvfb_get_option(struct fb_info *info) · line 982

Quoted 6 lines
drivers/video/fbdev/hyperv_fb.c +5 -0
+#include <linux/dma-contiguous.h>
 
+	/* Allocate from CMA */
+	// request_pages = (request_size >> PAGE_SHIFT) + 1;
+	request_pages = (round_up(request_size, PAGE_SIZE) >> PAGE_SHIFT);
+	page = dma_alloc_from_contiguous(NULL, request_pages, 0, false);

dma_alloc_from_contiguous is an internal helper, you must use it through dma_alloc_coherent and pass a struct device to that function.

drivers/video/fbdev/hyperv_fb.c static int hvfb_getmem(struct hv_device *hdev, struct fb_info *info) · line 1030

Quoted 8 lines
drivers/video/fbdev/hyperv_fb.c +8 -0
+	if (!gen2vm) {
+		pdev = pci_get_device(PCI_VENDOR_ID_MICROSOFT,
+			PCI_DEVICE_ID_HYPERV_VIDEO, NULL);
+		if (!pdev) {
+			pr_err("Unable to find PCI Hyper-V video\n");
+			return -ENODEV;
+		}
+	}

Please actually implement a pci_driver instead of hacks like this.

drivers/video/fbdev/hyperv_fb.c static int hvfb_getmem(struct hv_device *hdev, struct fb_info *info) · line 1063

Quoted 3 lines
drivers/video/fbdev/hyperv_fb.c +3 -0
+			par->need_docopy = false;
+			goto getmem1;
+		} else {

No need for an else after a goto.

author maintainer
#2 lore raw

Thanks for the review. Please see my response inline.

Quoted 8 lines

drivers/video/fbdev/Kconfig config FB_HYPERV · line 2218

Quoted 1 line
drivers/video/fbdev/Kconfig +1 -0
+	select DMA_CMA

Thіs needs to be

select DMA_CMA if HAVE_DMA_CONTIGUOUS

Quoted 1 line

+#include <linux/dma-contiguous.h>

Quoted 8 lines

drivers/video/fbdev/hyperv_fb.c static void hvfb_get_option(struct fb_info *info) · line 982

Quoted 4 lines
drivers/video/fbdev/hyperv_fb.c +4 -0
+	/* Allocate from CMA */
+	// request_pages = (request_size >> PAGE_SHIFT) + 1;
+	request_pages = (round_up(request_size, PAGE_SIZE) >> PAGE_SHIFT);
+	page = dma_alloc_from_contiguous(NULL, request_pages, 0, false);

dma_alloc_from_contiguous is an internal helper, you must use it through dma_alloc_coherent and pass a struct device to that function.

Can I directly use cma_alloc() and cma_release() in this case? The contiguous memory allocated is just for virtual framebuffer device, not for any DMA operation. I think using dma_alloc_coherent() might be a bit of overkill.

Quoted 11 lines

drivers/video/fbdev/hyperv_fb.c static int hvfb_getmem(struct hv_device *hdev, struct fb_info *info) · line 1030

Quoted 8 lines
drivers/video/fbdev/hyperv_fb.c +8 -0
+	if (!gen2vm) {
+		pdev = pci_get_device(PCI_VENDOR_ID_MICROSOFT,
+			PCI_DEVICE_ID_HYPERV_VIDEO, NULL);
+		if (!pdev) {
+			pr_err("Unable to find PCI Hyper-V video\n");
+			return -ENODEV;
+		}
+	}

Please actually implement a pci_driver instead of hacks like this.

I don't quite follow this. What do you mean implementing a pci_driver in this case?

Quoted 5 lines

drivers/video/fbdev/hyperv_fb.c static int hvfb_getmem(struct hv_device *hdev, struct fb_info *info) · line 1063

Quoted 3 lines
drivers/video/fbdev/hyperv_fb.c +3 -0
+			par->need_docopy = false;
+			goto getmem1;
+		} else {

No need for an else after a goto.

Thanks. Will do.

Wei
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#3 lore raw
In reply to Wei Hu

From: Wei Hu <[email protected]> Sent: Tuesday, October 22, 2019 4:11 AM

Quoted 18 lines

On Hyper-V, Generation 1 VMs can directly use VM's physical memory for their framebuffers. This can improve the efficiency of framebuffer and overall performence for VM. The physical memory assigned to framebuffer must be contiguous. We use CMA allocator to get contiguouse physicial memory when the framebuffer size is greater than 4MB. For size under 4MB, we use alloc_pages to achieve this.

To enable framebuffer memory allocation from CMA, supply a kernel parameter to give enough space to CMA allocator at boot time. For example:

    cma=130m

This gives 130MB memory to CAM allocator that can be allocated to framebuffer. If this fails, we fall back to the old way of using mmio for framebuffer.

---

[snip]

drivers/video/fbdev/hyperv_fb.c static void hvfb_get_option(struct fb_info *info) · line 963

Quoted 9 lines
drivers/video/fbdev/hyperv_fb.c +9 -0
+/*
+ * Allocate enough contiguous physical memory.
+ * Return physical address if succeeded or -1 if failed.
+ */
+static unsigned long hvfb_get_phymem(unsigned int request_size)
+{
+	struct page *page = NULL;
+	unsigned int request_pages;
+	unsigned long paddr = 0;

Using 'unsigned long' for physical addresses is problematic on 32-bit systems with Physical Address Extension (PAE) enabled. Linux has phys_addr_t that handles 32-bit PAE correctly and that probably
should be used here. This whole driver needs to be carefully
checked for the correct type for physical addresses.

Quoted 24 lines
drivers/video/fbdev/hyperv_fb.c +24 -0
+	unsigned int order = get_order(request_size);
+
+	if (request_size == 0)
+		return -1;
+
+	/* Try call alloc_pages if the size is less than 2^MAX_ORDER */
+	if (order < MAX_ORDER) {
+		page = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
+		if (!page)
+			return -1;
+
+		request_pages = (1 << order);
+		goto get_phymem1;
+	}
+
+	/* Allocate from CMA */
+	// request_pages = (request_size >> PAGE_SHIFT) + 1;
+	request_pages = (round_up(request_size, PAGE_SIZE) >> PAGE_SHIFT);
+	page = dma_alloc_from_contiguous(NULL, request_pages, 0, false);
+
+	if (page == NULL)
+		return -1;
+
+get_phymem1:

drivers/video/fbdev/hyperv_fb.c static void hvfb_get_option(struct fb_info *info) · line 997

Quoted 10 lines
drivers/video/fbdev/hyperv_fb.c +10 -0
+	paddr = (page_to_pfn(page) << PAGE_SHIFT);
+
+	pr_info("Allocated %d pages starts at physical addr 0x%lx\n",
+		request_pages, paddr);
+
+	return paddr;
+}
+
+/* Release contiguous physical memory */
+static void hvfb_release_phymem(unsigned long paddr, unsigned int size)

Same issue here with 'unsigned long paddr'.

Quoted 17 lines
drivers/video/fbdev/hyperv_fb.c +13 -0
+{
+	unsigned int order = get_order(size);
+
+	if (order < MAX_ORDER)
+		__free_pages(pfn_to_page(paddr >> PAGE_SHIFT), order);
+	else
+		dma_release_from_contiguous(NULL,
+					    pfn_to_page(paddr >> PAGE_SHIFT),
+					    (round_up(size, PAGE_SIZE) >>
+					     PAGE_SHIFT));
+					    // (size >> PAGE_SHIFT) + 1);
+}
+
 
 /* Get framebuffer memory from Hyper-V video pci space */
 static int hvfb_getmem(struct hv_device *hdev, struct fb_info *info)

drivers/video/fbdev/hyperv_fb.c static int hvfb_getmem(struct hv_device *hdev, struct fb_info · line 1021

Quoted 5 lines
drivers/video/fbdev/hyperv_fb.c +1 -0
@@ -947,8 +1017,58 @@ static int hvfb_getmem(struct hv_device *hdev, struct fb_info
 *info)
 	void __iomem *fb_virt;
 	int gen2vm = efi_enabled(EFI_BOOT);
 	resource_size_t pot_start, pot_end;
+	unsigned long paddr;

Same issue with 'unsigned long'.

Quoted 22 lines
drivers/video/fbdev/hyperv_fb.c +20 -0
 	int ret;
 
+	if (!gen2vm) {
+		pdev = pci_get_device(PCI_VENDOR_ID_MICROSOFT,
+			PCI_DEVICE_ID_HYPERV_VIDEO, NULL);
+		if (!pdev) {
+			pr_err("Unable to find PCI Hyper-V video\n");
+			return -ENODEV;
+		}
+	}
+
+	info->apertures = alloc_apertures(1);
+	if (!info->apertures)
+		goto err1;
+
+	if (gen2vm) {
+		info->apertures->ranges[0].base = screen_info.lfb_base;
+		info->apertures->ranges[0].size = screen_info.lfb_size;
+	} else {
+		info->apertures->ranges[0].base = pci_resource_start(pdev, 0);
+		info->apertures->ranges[0].size = pci_resource_len(pdev, 0);
+	}

drivers/video/fbdev/hyperv_fb.c static int hvfb_getmem(struct hv_device *hdev, struct fb_info *info) · line 1052

Quoted 10 lines
drivers/video/fbdev/hyperv_fb.c +10 -0
+
+	/*
+	 * For Gen 1 VM, we can directly use the contiguous memory
+	 * from VM. If we success, deferred IO happens directly
+	 * on this allocated framebuffer memory, avoiding extra
+	 * memory copy.
+	 */
+	if (!gen2vm) {
+		paddr = hvfb_get_phymem(screen_fb_size);
+		if (paddr != (unsigned long) -1) {

phys_addr_t

drivers/video/fbdev/hyperv_fb.c static int hvfb_getmem(struct hv_device *hdev, struct fb_info *info) · line 1053

Quoted 1 line
drivers/video/fbdev/hyperv_fb.c +1 -0
+			par->mmio_pp = paddr;

I'm not really sure what to do about the above, because mmio_pp is declared as 'unsigned long' when it really should be phys_addr_t. But maybe a MMIO address will always be less than 4 Gb and therefore will fit in 32 bits?

drivers/video/fbdev/hyperv_fb.c static int hvfb_getmem(struct hv_device *hdev, struct fb_info *info) · line 1056

Quoted 3 lines
drivers/video/fbdev/hyperv_fb.c +3 -0
+			par->mmio_vp = par->dio_vp = __va(paddr);
+
+			info->fix.smem_start = paddr;

smem_start is also declared as 'unsigned long', which seems problematic with 32-bit PAE. There are a lot of drivers that cast values as 'unsigned long' before storing into smem_start

Quoted 45 lines
drivers/video/fbdev/hyperv_fb.c +15 -0
+			info->fix.smem_len = screen_fb_size;
+			info->screen_base = par->mmio_vp;
+			info->screen_size = screen_fb_size;
+
+			par->need_docopy = false;
+			goto getmem1;
+		} else {
+			pr_info("Unable to allocate enough contiguous physical memory on
 Gen 1 VM. Use MMIO instead.\n");
+		}
+	}
+
+	/*
+	 * Cannot use the contiguous physical memory.
+	 * Allocate mmio space for framebuffer.
+	 */
 	dio_fb_size =
drivers/video/fbdev/hyperv_fb.c +0 -7
 		screen_width * screen_height * screen_depth / 8;
@@ -956,13 +1076,6 @@ static int hvfb_getmem(struct hv_device *hdev, struct fb_info
 *info)
 		pot_start = 0;
 		pot_end = -1;
 	} else {
-		pdev = pci_get_device(PCI_VENDOR_ID_MICROSOFT,
-			      PCI_DEVICE_ID_HYPERV_VIDEO, NULL);
-		if (!pdev) {
-			pr_err("Unable to find PCI Hyper-V video\n");
-			return -ENODEV;
-		}
-
 		if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM) ||
 		    pci_resource_len(pdev, 0) < screen_fb_size) {
drivers/video/fbdev/hyperv_fb.c +0 -6
 			pr_err("Resource not available or (0x%lx < 0x%lx)\n",
@@ -991,20 +1104,6 @@ static int hvfb_getmem(struct hv_device *hdev, struct fb_info
 *info)
 	if (par->dio_vp == NULL)
 		goto err3;
 
-	info->apertures = alloc_apertures(1);
-	if (!info->apertures)
-		goto err4;
-
-	if (gen2vm) {
-		info->apertures->ranges[0].base = screen_info.lfb_base;

drivers/video/fbdev/hyperv_fb.c static int hvfb_getmem(struct hv_device *hdev, struct fb_info · line 1109

Quoted 10 lines
drivers/video/fbdev/hyperv_fb.c +0 -8
-		info->apertures->ranges[0].size = screen_info.lfb_size;
-		remove_conflicting_framebuffers(info->apertures,
-						KBUILD_MODNAME, false);
-	} else {
-		info->apertures->ranges[0].base = pci_resource_start(pdev, 0);
-		info->apertures->ranges[0].size = pci_resource_len(pdev, 0);
-	}
-
 	/* Physical address of FB device */
 	par->mmio_pp = par->mem->start;

32-bit PAE problem could also occur here, even though the above line isn't part of your patch.

Ideally, we would build the kernel in 32-bit mode with PAE enabled, and test in an environment where the frame buffer gets allocated above the 4 Gbyte line. There may be some similar bugs in other parts of this driver that aren't touched by the patch.

Michael

maintainer
#4 lore raw
In reply to Wei Hu

drivers/video/fbdev/hyperv_fb.c static void hvfb_get_option(struct fb_info *info) · line 980

Quoted 6 lines

From: [email protected] <[email protected]> On Behalf Of Wei Hu
Sent: Tuesday, October 22, 2019 4:11 AM
...

drivers/video/fbdev/hyperv_fb.c +2 -0
+	/* Allocate from CMA */
+	// request_pages = (request_size >> PAGE_SHIFT) + 1;

"//" seems rare in Linux kernel code.
IMO "/* */" is more common.

Quoted 2 lines
(quoted diff) +1 -0
+			pr_info("Unable to allocate enough contiguous physical memory

on Gen 1 VM. Use MMIO instead.\n");

The line exceeds 80 chars.

drivers/video/fbdev/hyperv_fb.c static int hvfb_probe(struct hv_device *hdev, · line 1171

Quoted 5 lines
drivers/video/fbdev/hyperv_fb.c +1 -0
@@ -1060,6 +1168,7 @@ static int hvfb_probe(struct hv_device *hdev,
 	par = info->par;
 	par->info = info;
 	par->fb_ready = false;
+	par->need_docopy = false;

Maybe it's better if we set the default value to true? This way we can save
the " par->need_docopy = true;" in hvfb_getmem().

Thanks,
-- Dexuan