Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) #include <linux/cred.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) #include <linux/dma-buf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <linux/highmem.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/memfd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/miscdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/shmem_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/udmabuf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) static const u32    list_limit = 1024;  /* udmabuf_create_list->count limit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) static const size_t size_limit_mb = 64; /* total dmabuf size, in megabytes  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) struct udmabuf {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 	pgoff_t pagecount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 	struct page **pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	struct sg_table *sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	struct miscdevice *device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	struct vm_area_struct *vma = vmf->vma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	struct udmabuf *ubuf = vma->vm_private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	vmf->page = ubuf->pages[vmf->pgoff];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	get_page(vmf->page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) static const struct vm_operations_struct udmabuf_vm_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	.fault = udmabuf_vm_fault,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) static int mmap_udmabuf(struct dma_buf *buf, struct vm_area_struct *vma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	struct udmabuf *ubuf = buf->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	if ((vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	vma->vm_ops = &udmabuf_vm_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	vma->vm_private_data = ubuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) static struct sg_table *get_sg_table(struct device *dev, struct dma_buf *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 				     enum dma_data_direction direction)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	struct udmabuf *ubuf = buf->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	struct sg_table *sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	sg = kzalloc(sizeof(*sg), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	if (!sg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	ret = sg_alloc_table_from_pages(sg, ubuf->pages, ubuf->pagecount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 					0, ubuf->pagecount << PAGE_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 					GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	ret = dma_map_sgtable(dev, sg, direction, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	return sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	sg_free_table(sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	kfree(sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) static void put_sg_table(struct device *dev, struct sg_table *sg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 			 enum dma_data_direction direction)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	dma_unmap_sgtable(dev, sg, direction, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	sg_free_table(sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	kfree(sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) static struct sg_table *map_udmabuf(struct dma_buf_attachment *at,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 				    enum dma_data_direction direction)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	return get_sg_table(at->dev, at->dmabuf, direction);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) static void unmap_udmabuf(struct dma_buf_attachment *at,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 			  struct sg_table *sg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 			  enum dma_data_direction direction)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	return put_sg_table(at->dev, sg, direction);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) static void release_udmabuf(struct dma_buf *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	struct udmabuf *ubuf = buf->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	struct device *dev = ubuf->device->this_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	pgoff_t pg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	if (ubuf->sg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		put_sg_table(dev, ubuf->sg, DMA_BIDIRECTIONAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	for (pg = 0; pg < ubuf->pagecount; pg++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		put_page(ubuf->pages[pg]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	kfree(ubuf->pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	kfree(ubuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) static int begin_cpu_udmabuf(struct dma_buf *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 			     enum dma_data_direction direction)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	struct udmabuf *ubuf = buf->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	struct device *dev = ubuf->device->this_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	if (!ubuf->sg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		ubuf->sg = get_sg_table(dev, buf, direction);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		if (IS_ERR(ubuf->sg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 			return PTR_ERR(ubuf->sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		dma_sync_sg_for_cpu(dev, ubuf->sg->sgl, ubuf->sg->nents,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 				    direction);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) static int end_cpu_udmabuf(struct dma_buf *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 			   enum dma_data_direction direction)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	struct udmabuf *ubuf = buf->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	struct device *dev = ubuf->device->this_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	if (!ubuf->sg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	dma_sync_sg_for_device(dev, ubuf->sg->sgl, ubuf->sg->nents, direction);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) static const struct dma_buf_ops udmabuf_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	.cache_sgt_mapping = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	.map_dma_buf	   = map_udmabuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	.unmap_dma_buf	   = unmap_udmabuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	.release	   = release_udmabuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	.mmap		   = mmap_udmabuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	.begin_cpu_access  = begin_cpu_udmabuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	.end_cpu_access    = end_cpu_udmabuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) #define SEALS_WANTED (F_SEAL_SHRINK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) #define SEALS_DENIED (F_SEAL_WRITE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static long udmabuf_create(struct miscdevice *device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 			   struct udmabuf_create_list *head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 			   struct udmabuf_create_item *list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	struct file *memfd = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	struct udmabuf *ubuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	struct dma_buf *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	pgoff_t pgoff, pgcnt, pgidx, pgbuf = 0, pglimit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	int seals, ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	u32 i, flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	ubuf = kzalloc(sizeof(*ubuf), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	if (!ubuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	pglimit = (size_limit_mb * 1024 * 1024) >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	for (i = 0; i < head->count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		if (!IS_ALIGNED(list[i].offset, PAGE_SIZE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		if (!IS_ALIGNED(list[i].size, PAGE_SIZE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		ubuf->pagecount += list[i].size >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		if (ubuf->pagecount > pglimit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	if (!ubuf->pagecount)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	ubuf->pages = kmalloc_array(ubuf->pagecount, sizeof(*ubuf->pages),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 				    GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	if (!ubuf->pages) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	pgbuf = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	for (i = 0; i < head->count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		ret = -EBADFD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		memfd = fget(list[i].memfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		if (!memfd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		if (!shmem_mapping(file_inode(memfd)->i_mapping))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		seals = memfd_fcntl(memfd, F_GET_SEALS, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		if (seals == -EINVAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		if ((seals & SEALS_WANTED) != SEALS_WANTED ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		    (seals & SEALS_DENIED) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 		pgoff = list[i].offset >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		pgcnt = list[i].size   >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		for (pgidx = 0; pgidx < pgcnt; pgidx++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 			page = shmem_read_mapping_page(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 				file_inode(memfd)->i_mapping, pgoff + pgidx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 			if (IS_ERR(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 				ret = PTR_ERR(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 				goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 			ubuf->pages[pgbuf++] = page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		fput(memfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		memfd = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	exp_info.ops  = &udmabuf_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	exp_info.size = ubuf->pagecount << PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	exp_info.priv = ubuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	exp_info.flags = O_RDWR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	ubuf->device = device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	buf = dma_buf_export(&exp_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	if (IS_ERR(buf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		ret = PTR_ERR(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	if (head->flags & UDMABUF_FLAGS_CLOEXEC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		flags |= O_CLOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	return dma_buf_fd(buf, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	while (pgbuf > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		put_page(ubuf->pages[--pgbuf]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	if (memfd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		fput(memfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	kfree(ubuf->pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	kfree(ubuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) static long udmabuf_ioctl_create(struct file *filp, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	struct udmabuf_create create;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	struct udmabuf_create_list head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	struct udmabuf_create_item list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	if (copy_from_user(&create, (void __user *)arg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 			   sizeof(create)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	head.flags  = create.flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	head.count  = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	list.memfd  = create.memfd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	list.offset = create.offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	list.size   = create.size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	return udmabuf_create(filp->private_data, &head, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) static long udmabuf_ioctl_create_list(struct file *filp, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	struct udmabuf_create_list head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	struct udmabuf_create_item *list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	int ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	u32 lsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	if (copy_from_user(&head, (void __user *)arg, sizeof(head)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	if (head.count > list_limit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	lsize = sizeof(struct udmabuf_create_item) * head.count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	list = memdup_user((void __user *)(arg + sizeof(head)), lsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	if (IS_ERR(list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		return PTR_ERR(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	ret = udmabuf_create(filp->private_data, &head, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	kfree(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) static long udmabuf_ioctl(struct file *filp, unsigned int ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 			  unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	switch (ioctl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	case UDMABUF_CREATE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		ret = udmabuf_ioctl_create(filp, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	case UDMABUF_CREATE_LIST:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		ret = udmabuf_ioctl_create_list(filp, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		ret = -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) static const struct file_operations udmabuf_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	.owner		= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	.unlocked_ioctl = udmabuf_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	.compat_ioctl   = udmabuf_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) static struct miscdevice udmabuf_misc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	.minor          = MISC_DYNAMIC_MINOR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	.name           = "udmabuf",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	.fops           = &udmabuf_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) static int __init udmabuf_dev_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	return misc_register(&udmabuf_misc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) static void __exit udmabuf_dev_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	misc_deregister(&udmabuf_misc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) module_init(udmabuf_dev_init)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) module_exit(udmabuf_dev_exit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) MODULE_AUTHOR("Gerd Hoffmann <kraxel@redhat.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) MODULE_LICENSE("GPL v2");