tee: enable support to register kernel memory
authorSumit Garg <sumit.garg@linaro.org>
Fri, 27 Mar 2020 05:29:47 +0000 (10:59 +0530)
committerJens Wiklander <jens.wiklander@linaro.org>
Mon, 20 Apr 2020 13:49:56 +0000 (15:49 +0200)
Enable support to register kernel memory reference with TEE. This change
will allow TEE bus drivers to register memory references.

Signed-off-by: Sumit Garg <sumit.garg@linaro.org>
Signed-off-by: Jens Wiklander <jens.wiklander@linaro.org>
drivers/tee/tee_shm.c
include/linux/tee_drv.h

index bd679b72bd05ce7ce35061fac83f996feabead8f..c259271d4d5f30495d2b38b062a7afecc9b4930e 100644 (file)
@@ -9,6 +9,7 @@
 #include <linux/sched.h>
 #include <linux/slab.h>
 #include <linux/tee_drv.h>
+#include <linux/uio.h>
 #include "tee_private.h"
 
 static void tee_shm_release(struct tee_shm *shm)
@@ -185,14 +186,15 @@ struct tee_shm *tee_shm_register(struct tee_context *ctx, unsigned long addr,
                                 size_t length, u32 flags)
 {
        struct tee_device *teedev = ctx->teedev;
-       const u32 req_flags = TEE_SHM_DMA_BUF | TEE_SHM_USER_MAPPED;
+       const u32 req_user_flags = TEE_SHM_DMA_BUF | TEE_SHM_USER_MAPPED;
+       const u32 req_kernel_flags = TEE_SHM_DMA_BUF | TEE_SHM_KERNEL_MAPPED;
        struct tee_shm *shm;
        void *ret;
        int rc;
        int num_pages;
        unsigned long start;
 
-       if (flags != req_flags)
+       if (flags != req_user_flags && flags != req_kernel_flags)
                return ERR_PTR(-ENOTSUPP);
 
        if (!tee_device_get(teedev))
@@ -226,7 +228,27 @@ struct tee_shm *tee_shm_register(struct tee_context *ctx, unsigned long addr,
                goto err;
        }
 
-       rc = get_user_pages_fast(start, num_pages, FOLL_WRITE, shm->pages);
+       if (flags & TEE_SHM_USER_MAPPED) {
+               rc = get_user_pages_fast(start, num_pages, FOLL_WRITE,
+                                        shm->pages);
+       } else {
+               struct kvec *kiov;
+               int i;
+
+               kiov = kcalloc(num_pages, sizeof(*kiov), GFP_KERNEL);
+               if (!kiov) {
+                       ret = ERR_PTR(-ENOMEM);
+                       goto err;
+               }
+
+               for (i = 0; i < num_pages; i++) {
+                       kiov[i].iov_base = (void *)(start + i * PAGE_SIZE);
+                       kiov[i].iov_len = PAGE_SIZE;
+               }
+
+               rc = get_kernel_pages(kiov, num_pages, 0, shm->pages);
+               kfree(kiov);
+       }
        if (rc > 0)
                shm->num_pages = rc;
        if (rc != num_pages) {
index 1412e9cc79ce9b73a9a03c3ea8a2eb359e400dac..e96154d92b1ecd34dd9f446c9ec2e417d4469f99 100644 (file)
@@ -26,6 +26,7 @@
 #define TEE_SHM_REGISTER       BIT(3)  /* Memory registered in secure world */
 #define TEE_SHM_USER_MAPPED    BIT(4)  /* Memory mapped in user space */
 #define TEE_SHM_POOL           BIT(5)  /* Memory allocated from pool */
+#define TEE_SHM_KERNEL_MAPPED  BIT(6)  /* Memory mapped in kernel space */
 
 struct device;
 struct tee_device;