@@ -68,21 +68,29 @@ static int mmap_udmabuf(struct dma_buf *buf, struct vm_area_struct *vma)
6868static int vmap_udmabuf (struct dma_buf * buf , struct iosys_map * map )
6969{
7070 struct udmabuf * ubuf = buf -> priv ;
71- struct page * * pages ;
71+ unsigned long * pfns ;
7272 void * vaddr ;
7373 pgoff_t pg ;
7474
7575 dma_resv_assert_held (buf -> resv );
7676
77- pages = kmalloc_array (ubuf -> pagecount , sizeof (* pages ), GFP_KERNEL );
78- if (!pages )
77+ /**
78+ * HVO may free tail pages, so just use pfn to map each folio
79+ * into vmalloc area.
80+ */
81+ pfns = kvmalloc_array (ubuf -> pagecount , sizeof (* pfns ), GFP_KERNEL );
82+ if (!pfns )
7983 return - ENOMEM ;
8084
81- for (pg = 0 ; pg < ubuf -> pagecount ; pg ++ )
82- pages [pg ] = & ubuf -> folios [pg ]-> page ;
85+ for (pg = 0 ; pg < ubuf -> pagecount ; pg ++ ) {
86+ unsigned long pfn = folio_pfn (ubuf -> folios [pg ]);
87+
88+ pfn += ubuf -> offsets [pg ] >> PAGE_SHIFT ;
89+ pfns [pg ] = pfn ;
90+ }
8391
84- vaddr = vm_map_ram ( pages , ubuf -> pagecount , -1 );
85- kfree ( pages );
92+ vaddr = vmap_pfn ( pfns , ubuf -> pagecount , PAGE_KERNEL );
93+ kvfree ( pfns );
8694 if (!vaddr )
8795 return - EINVAL ;
8896
@@ -164,8 +172,8 @@ static void release_udmabuf(struct dma_buf *buf)
164172
165173 for (pg = 0 ; pg < ubuf -> pagecount ; pg ++ )
166174 folio_put (ubuf -> folios [pg ]);
167- kfree (ubuf -> offsets );
168- kfree (ubuf -> folios );
175+ kvfree (ubuf -> offsets );
176+ kvfree (ubuf -> folios );
169177 kfree (ubuf );
170178}
171179
@@ -216,7 +224,7 @@ static const struct dma_buf_ops udmabuf_ops = {
216224};
217225
218226#define SEALS_WANTED (F_SEAL_SHRINK)
219- #define SEALS_DENIED (F_SEAL_WRITE)
227+ #define SEALS_DENIED (F_SEAL_WRITE|F_SEAL_FUTURE_WRITE )
220228
221229static int handle_hugetlb_pages (struct udmabuf * ubuf , struct file * memfd ,
222230 pgoff_t offset , pgoff_t pgcnt ,
@@ -297,24 +305,18 @@ static int check_memfd_seals(struct file *memfd)
297305 return 0 ;
298306}
299307
300- static int export_udmabuf (struct udmabuf * ubuf ,
301- struct miscdevice * device ,
302- u32 flags )
308+ static struct dma_buf * export_udmabuf (struct udmabuf * ubuf ,
309+ struct miscdevice * device )
303310{
304311 DEFINE_DMA_BUF_EXPORT_INFO (exp_info );
305- struct dma_buf * buf ;
306312
307313 ubuf -> device = device ;
308314 exp_info .ops = & udmabuf_ops ;
309315 exp_info .size = ubuf -> pagecount << PAGE_SHIFT ;
310316 exp_info .priv = ubuf ;
311317 exp_info .flags = O_RDWR ;
312318
313- buf = dma_buf_export (& exp_info );
314- if (IS_ERR (buf ))
315- return PTR_ERR (buf );
316-
317- return dma_buf_fd (buf , flags );
319+ return dma_buf_export (& exp_info );
318320}
319321
320322static long udmabuf_create (struct miscdevice * device ,
@@ -324,6 +326,7 @@ static long udmabuf_create(struct miscdevice *device,
324326 pgoff_t pgcnt , pgbuf = 0 , pglimit ;
325327 struct file * memfd = NULL ;
326328 struct udmabuf * ubuf ;
329+ struct dma_buf * dmabuf ;
327330 int ret = - EINVAL ;
328331 u32 i , flags ;
329332
@@ -345,14 +348,14 @@ static long udmabuf_create(struct miscdevice *device,
345348 if (!ubuf -> pagecount )
346349 goto err ;
347350
348- ubuf -> folios = kmalloc_array (ubuf -> pagecount , sizeof (* ubuf -> folios ),
349- GFP_KERNEL );
351+ ubuf -> folios = kvmalloc_array (ubuf -> pagecount , sizeof (* ubuf -> folios ),
352+ GFP_KERNEL );
350353 if (!ubuf -> folios ) {
351354 ret = - ENOMEM ;
352355 goto err ;
353356 }
354- ubuf -> offsets = kcalloc (ubuf -> pagecount , sizeof (* ubuf -> offsets ),
355- GFP_KERNEL );
357+ ubuf -> offsets = kvcalloc (ubuf -> pagecount , sizeof (* ubuf -> offsets ),
358+ GFP_KERNEL );
356359 if (!ubuf -> offsets ) {
357360 ret = - ENOMEM ;
358361 goto err ;
@@ -382,9 +385,20 @@ static long udmabuf_create(struct miscdevice *device,
382385 }
383386
384387 flags = head -> flags & UDMABUF_FLAGS_CLOEXEC ? O_CLOEXEC : 0 ;
385- ret = export_udmabuf (ubuf , device , flags );
386- if (ret < 0 )
388+ dmabuf = export_udmabuf (ubuf , device );
389+ if (IS_ERR (dmabuf )) {
390+ ret = PTR_ERR (dmabuf );
387391 goto err ;
392+ }
393+ /*
394+ * Ownership of ubuf is held by the dmabuf from here.
395+ * If the following dma_buf_fd() fails, dma_buf_put() cleans up both the
396+ * dmabuf and the ubuf (through udmabuf_ops.release).
397+ */
398+
399+ ret = dma_buf_fd (dmabuf , flags );
400+ if (ret < 0 )
401+ dma_buf_put (dmabuf );
388402
389403 return ret ;
390404
@@ -393,8 +407,8 @@ static long udmabuf_create(struct miscdevice *device,
393407 folio_put (ubuf -> folios [-- pgbuf ]);
394408 if (memfd )
395409 fput (memfd );
396- kfree (ubuf -> offsets );
397- kfree (ubuf -> folios );
410+ kvfree (ubuf -> offsets );
411+ kvfree (ubuf -> folios );
398412 kfree (ubuf );
399413 return ret ;
400414}
0 commit comments