Merge tag 'dt-5.15' of git://git.kernel.org/pub/scm/linux/kernel/git/soc/soc
[linux-2.6-microblaze.git] / drivers / dma-buf / udmabuf.c
index db732f7..c57a609 100644 (file)
 #include <linux/shmem_fs.h>
 #include <linux/slab.h>
 #include <linux/udmabuf.h>
+#include <linux/hugetlb.h>
 
-static const u32    list_limit = 1024;  /* udmabuf_create_list->count limit */
-static const size_t size_limit_mb = 64; /* total dmabuf size, in megabytes  */
+static int list_limit = 1024;
+module_param(list_limit, int, 0644);
+MODULE_PARM_DESC(list_limit, "udmabuf_create_list->count limit. Default is 1024.");
+
+static int size_limit_mb = 64;
+module_param(size_limit_mb, int, 0644);
+MODULE_PARM_DESC(size_limit_mb, "Max size of a dmabuf, in megabytes. Default is 64.");
 
 struct udmabuf {
        pgoff_t pagecount;
@@ -160,10 +166,13 @@ static long udmabuf_create(struct miscdevice *device,
 {
        DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
        struct file *memfd = NULL;
+       struct address_space *mapping = NULL;
        struct udmabuf *ubuf;
        struct dma_buf *buf;
        pgoff_t pgoff, pgcnt, pgidx, pgbuf = 0, pglimit;
-       struct page *page;
+       struct page *page, *hpage = NULL;
+       pgoff_t subpgoff, maxsubpgs;
+       struct hstate *hpstate;
        int seals, ret = -EINVAL;
        u32 i, flags;
 
@@ -194,7 +203,8 @@ static long udmabuf_create(struct miscdevice *device,
                memfd = fget(list[i].memfd);
                if (!memfd)
                        goto err;
-               if (!shmem_mapping(file_inode(memfd)->i_mapping))
+               mapping = file_inode(memfd)->i_mapping;
+               if (!shmem_mapping(mapping) && !is_file_hugepages(memfd))
                        goto err;
                seals = memfd_fcntl(memfd, F_GET_SEALS, 0);
                if (seals == -EINVAL)
@@ -205,17 +215,48 @@ static long udmabuf_create(struct miscdevice *device,
                        goto err;
                pgoff = list[i].offset >> PAGE_SHIFT;
                pgcnt = list[i].size   >> PAGE_SHIFT;
+               if (is_file_hugepages(memfd)) {
+                       hpstate = hstate_file(memfd);
+                       pgoff = list[i].offset >> huge_page_shift(hpstate);
+                       subpgoff = (list[i].offset &
+                                   ~huge_page_mask(hpstate)) >> PAGE_SHIFT;
+                       maxsubpgs = huge_page_size(hpstate) >> PAGE_SHIFT;
+               }
                for (pgidx = 0; pgidx < pgcnt; pgidx++) {
-                       page = shmem_read_mapping_page(
-                               file_inode(memfd)->i_mapping, pgoff + pgidx);
-                       if (IS_ERR(page)) {
-                               ret = PTR_ERR(page);
-                               goto err;
+                       if (is_file_hugepages(memfd)) {
+                               if (!hpage) {
+                                       hpage = find_get_page_flags(mapping, pgoff,
+                                                                   FGP_ACCESSED);
+                                       if (!hpage) {
+                                               ret = -EINVAL;
+                                               goto err;
+                                       }
+                               }
+                               page = hpage + subpgoff;
+                               get_page(page);
+                               subpgoff++;
+                               if (subpgoff == maxsubpgs) {
+                                       put_page(hpage);
+                                       hpage = NULL;
+                                       subpgoff = 0;
+                                       pgoff++;
+                               }
+                       } else {
+                               page = shmem_read_mapping_page(mapping,
+                                                              pgoff + pgidx);
+                               if (IS_ERR(page)) {
+                                       ret = PTR_ERR(page);
+                                       goto err;
+                               }
                        }
                        ubuf->pages[pgbuf++] = page;
                }
                fput(memfd);
                memfd = NULL;
+               if (hpage) {
+                       put_page(hpage);
+                       hpage = NULL;
+               }
        }
 
        exp_info.ops  = &udmabuf_ops;