tee: make tee_shm_register_kernel_buf vmalloc supported
authorPhil Chang <phil.chang@mediatek.com>
Fri, 25 Feb 2022 15:20:40 +0000 (23:20 +0800)
committerJens Wiklander <jens.wiklander@linaro.org>
Tue, 5 Apr 2022 06:04:16 +0000 (08:04 +0200)
In some low-memory devices, it's hard to aquire large-orders pages,
this patch allowed user using scatter pages to register shm.

Signed-off-by: Phil Chang <phil.chang@mediatek.com>
Reviewed-by: Sumit Garg <sumit.garg@linaro.org>
Signed-off-by: Jens Wiklander <jens.wiklander@linaro.org>
drivers/tee/optee/call.c
drivers/tee/tee_shm.c

index bd49ec934060c25f2ddf19ebf4a9697e30e8dbc7..2082e632adff98f8011ebba26fb8eec2e421e88b 100644 (file)
@@ -362,7 +362,7 @@ int optee_check_mem_type(unsigned long start, size_t num_pages)
         * Allow kernel address to register with OP-TEE as kernel
         * pages are configured as normal memory only.
         */
-       if (virt_addr_valid(start))
+       if (virt_addr_valid(start) || is_vmalloc_addr((void *)start))
                return 0;
 
        mmap_read_lock(mm);
index f31e29e8f1cac74200a6714c46d1ac9b0f0ccbc4..836872467dc64a6d682888f10313001c79208040 100644 (file)
@@ -23,21 +23,36 @@ static void shm_put_kernel_pages(struct page **pages, size_t page_count)
 static int shm_get_kernel_pages(unsigned long start, size_t page_count,
                                struct page **pages)
 {
-       struct kvec *kiov;
        size_t n;
        int rc;
 
-       kiov = kcalloc(page_count, sizeof(*kiov), GFP_KERNEL);
-       if (!kiov)
-               return -ENOMEM;
+       if (is_vmalloc_addr((void *)start)) {
+               struct page *page;
 
-       for (n = 0; n < page_count; n++) {
-               kiov[n].iov_base = (void *)(start + n * PAGE_SIZE);
-               kiov[n].iov_len = PAGE_SIZE;
-       }
+               for (n = 0; n < page_count; n++) {
+                       page = vmalloc_to_page((void *)(start + PAGE_SIZE * n));
+                       if (!page)
+                               return -ENOMEM;
+
+                       get_page(page);
+                       pages[n] = page;
+               }
+               rc = page_count;
+       } else {
+               struct kvec *kiov;
+
+               kiov = kcalloc(page_count, sizeof(*kiov), GFP_KERNEL);
+               if (!kiov)
+                       return -ENOMEM;
 
-       rc = get_kernel_pages(kiov, page_count, 0, pages);
-       kfree(kiov);
+               for (n = 0; n < page_count; n++) {
+                       kiov[n].iov_base = (void *)(start + n * PAGE_SIZE);
+                       kiov[n].iov_len = PAGE_SIZE;
+               }
+
+               rc = get_kernel_pages(kiov, page_count, 0, pages);
+               kfree(kiov);
+       }
 
        return rc;
 }