Skip to contentSkip to conversation

Fix udmabuf vmap error

PATCH New Huan Yang 1 participant 3 messages Apr 28, 2025

Subsystems (3)
lore
Share & tools
lore

Huan Yang

author
#0 lore raw

v2 cover Fix udmabuf vmap error

V2 version just change commit message, no functional change.

In [1] Bingbu report an error that vmap_udmabuf invoke failed due to use vmap_pfn and blocked by !pfn_valid.

Due to misunderstand it, I change vmap_pfn check of !pfn_valid to pfn_valid, then send [2], then Christoph point that:
vmap_pfn is entirely for memory not backed by pages or folios, i.e. PCIe BARs and similar memory. This must not be mixed with proper folio backed memory.

So, I given a misuse of vmap_pfn. But to fix vmap_udmabuf, and consider HVO effect, I offer a RFC patchset [3], which simple copy vmap_pfn to vmap_udmabuf, other implement folio range based vmap.

But in [3], Muchun point that I misunderstand HVO, which do not released any page struct pointer in vmemmap, only change this VA's PTE point to hugetlb's **HEAD** page frame, and release remainned **tail** page frame. So any page struct pointer do exist, and folio_page, folio_pfn or other api still can work.

By this, we can fix this error simpliy:
Patch 1 revert this vmap_pfn misuse patch.
Patch 2 fix missed offset page set.

ChangeLog:
v1->v2: change Patch1's commit message which vivek suggested.

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://lore.kernel.org/all/[email protected]/

Huan Yang (2):
Revert "udmabuf: fix vmap_udmabuf error page set"
udmabuf: fix vmap missed offset page

 drivers/dma-buf/Kconfig   |  1 -
 drivers/dma-buf/udmabuf.c | 23 ++++++++---------------
 2 files changed, 8 insertions(+), 16 deletions(-)

base-commit: b425262c07a6a643ebeed91046e161e20b944164 --
2.48.1

author
#1 lore raw

PATCH v2 1/2 Revert "udmabuf: fix vmap_udmabuf error page set"

This reverts commit 18d7de823b7150344d242c3677e65d68c5271b04.

We cannot use vmap_pfn() in vmap_udmabuf() as it would fail the pfn_valid() check in vmap_pfn_apply(). This is because vmap_pfn() is intended to be used for mapping non-struct-page memory such as PCIe BARs. Since, udmabuf mostly works with pages/folios backed by shmem/hugetlbfs/THP, vmap_pfn() is not the right tool or API to invoke for implementing vmap.

---
 drivers/dma-buf/Kconfig   |  1 -
 drivers/dma-buf/udmabuf.c | 22 +++++++---------------
 2 files changed, 7 insertions(+), 16 deletions(-)
drivers/dma-buf/Kconfig +0 -1
index fee04fdb0822..b46eb8a552d7 100644
@@ -36,7 +36,6 @@ config UDMABUF
 	depends on DMA_SHARED_BUFFER
 	depends on MEMFD_CREATE || COMPILE_TEST
 	depends on MMU
-	select VMAP_PFN
 	help
 	  A driver to let userspace turn memfd regions into dma-bufs.
 	  Qemu can use this to create host dmabufs for guest framebuffers.
drivers/dma-buf/udmabuf.c +7 -15
index 7eee3eb47a8e..79845565089d 100644
@@ -109,29 +109,21 @@ static int mmap_udmabuf(struct dma_buf *buf, struct vm_area_struct *vma)
 static int vmap_udmabuf(struct dma_buf *buf, struct iosys_map *map)
 {
 	struct udmabuf *ubuf = buf->priv;
-	unsigned long *pfns;
+	struct page **pages;
 	void *vaddr;
 	pgoff_t pg;
 
 	dma_resv_assert_held(buf->resv);
 
-	/**
-	 * HVO may free tail pages, so just use pfn to map each folio
-	 * into vmalloc area.
-	 */
-	pfns = kvmalloc_array(ubuf->pagecount, sizeof(*pfns), GFP_KERNEL);
-	if (!pfns)
+	pages = kvmalloc_array(ubuf->pagecount, sizeof(*pages), GFP_KERNEL);
+	if (!pages)
 		return -ENOMEM;
 
-	for (pg = 0; pg < ubuf->pagecount; pg++) {
-		unsigned long pfn = folio_pfn(ubuf->folios[pg]);
-
-		pfn += ubuf->offsets[pg] >> PAGE_SHIFT;
-		pfns[pg] = pfn;
-	}
+	for (pg = 0; pg < ubuf->pagecount; pg++)
+		pages[pg] = &ubuf->folios[pg]->page;
 
-	vaddr = vmap_pfn(pfns, ubuf->pagecount, PAGE_KERNEL);
-	kvfree(pfns);
+	vaddr = vm_map_ram(pages, ubuf->pagecount, -1);
+	kvfree(pages);
 	if (!vaddr)
 		return -EINVAL;
 
Signature · 1 line
2.48.1
author
#2 lore raw

PATCH v2 2/2 udmabuf: fix vmap missed offset page

In reply to Huan Yang

Before invoke vmap, we need offer a pages pointer array which each page need to map in vmalloc area.

But currently vmap_udmabuf only set each folio's head page into pages, missed each offset pages when iter.

This patch set the correctly offset page in each folio into array.

Fixes 5e72b2b41a21 ("udmabuf: convert udmabuf driver to use folios")
---
 drivers/dma-buf/udmabuf.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/dma-buf/udmabuf.c +2 -1
index 79845565089d..af5200e360a6 100644
@@ -120,7 +120,8 @@ static int vmap_udmabuf(struct dma_buf *buf, struct iosys_map *map)
 		return -ENOMEM;
 
 	for (pg = 0; pg < ubuf->pagecount; pg++)
-		pages[pg] = &ubuf->folios[pg]->page;
+		pages[pg] = folio_page(ubuf->folios[pg],
+				       ubuf->offsets[pg] >> PAGE_SHIFT);
 
 	vaddr = vm_map_ram(pages, ubuf->pagecount, -1);
 	kvfree(pages);
Signature · 1 line
2.48.1