Skip to contentSkip to conversation

udmabuf: fix vmap_udmabuf error page set

PATCH New Huan Yang 1 participant 2 messages Jul 31, 2024

Subsystems (3)
lore
Share & tools
lore

Huan Yang

author
#0 lore raw

PATCH 1/2 udmabuf: fix vmap_udmabuf error page set

Currently vmap_udmabuf set page's array by each folio. But, ubuf->folios is only contain's the folio's head page.

That mean we repeatedly mapped the folio head page to the vmalloc area.

This patch fix it, set each folio's page correct, so that pages array contains right page, and then map into vmalloc area

---
 drivers/dma-buf/udmabuf.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/dma-buf/udmabuf.c +2 -1
index 047c3cd2ceff..6604d91e7072 100644
@@ -85,7 +85,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);
 	kfree(pages);

base-commit: cd19ac2f903276b820f5d0d89de0c896c27036ed

Signature · 1 line
2.45.2
author
#1 lore raw

PATCH 2/2 udmabuf: make udmabuf folios and offsets set more readable

The current code for setting folios and offsets is not very readable.

In fact, udmabuf->folios represents the head page of each folio, while the offset indicates the PAGE_SIZE offset within the folio for the corresponding pgcnt. And folios only add head page(the folio) into unpin list.

This patch changes the loop condition to use folios as the outer loop and sets the subpages of the folio in the inner loop, making it easier to understand the relationship between unpin and folios/offset.

The loop conditions for j and k are actually only used in the head loop, so they can be turned into loop variables.

---
 drivers/dma-buf/udmabuf.c | 40 ++++++++++++++++++++-------------------
 1 file changed, 21 insertions(+), 19 deletions(-)
drivers/dma-buf/udmabuf.c +21 -19
index 6604d91e7072..0285194e6b51 100644
@@ -301,7 +301,7 @@ static long udmabuf_create(struct miscdevice *device,
 	struct file *memfd = NULL;
 	struct folio **folios;
 	struct udmabuf *ubuf;
-	u32 i, j, k, flags;
+	u32 i, flags;
 	loff_t end;
 
 	ubuf = kzalloc(sizeof(*ubuf), GFP_KERNEL);
@@ -338,6 +338,7 @@ static long udmabuf_create(struct miscdevice *device,
 
 	pgbuf = 0;
 	for (i = 0; i < head->count; i++) {
+		u32 j, k;
 		memfd = fget(list[i].memfd);
 		ret = check_memfd_seals(memfd);
 		if (ret < 0)
@@ -360,26 +361,27 @@ static long udmabuf_create(struct miscdevice *device,
 			goto err;
 		}
 
-		nr_folios = ret;
-		pgoff >>= PAGE_SHIFT;
-		for (j = 0, k = 0; j < pgcnt; j++) {
-			ubuf->folios[pgbuf] = folios[k];
-			ubuf->offsets[pgbuf] = pgoff << PAGE_SHIFT;
-
-			if (j == 0 || ubuf->folios[pgbuf-1] != folios[k]) {
-				ret = add_to_unpin_list(&ubuf->unpin_list,
-							folios[k]);
-				if (ret < 0) {
-					kfree(folios);
-					goto err;
-				}
+		/**
+		 * Iter each folios we got from pin, add each folio into unpin
+		 * list and setup each folio and page offset into folio into
+		 * corrent page count position.
+		 * So that we can iter any offset in size and get correctly
+		 * page.
+		 */
+		for (j = 0, pgoff >>= PAGE_SHIFT, nr_folios = ret;
+		     j < nr_folios; ++j, pgoff = 0) {
+			long nr_pages;
+
+			ret = add_to_unpin_list(&ubuf->unpin_list, folios[j]);
+			if (ret < 0) {
+				kfree(folios);
+				goto err;
 			}
 
-			pgbuf++;
-			if (++pgoff == folio_nr_pages(folios[k])) {
-				pgoff = 0;
-				if (++k == nr_folios)
-					break;
+			for (k = 0, nr_pages = folio_nr_pages(folios[j]);
+			     k < nr_pages; ++k, ++pgoff, ++pgbuf) {
+				ubuf->folios[pgbuf] = folios[j];
+				ubuf->offsets[pgbuf] = pgoff << PAGE_SHIFT;
 			}
 		}
 
Signature · 1 line
2.45.2