Add TEE_IOC_RSTMEM_FD_INFO to retrieve information about a previously
allocated restricted memory dma-buf file descriptor. This is needed if
the file descriptor from a restricted memory allocation has been saved
due to limitations in the application.

Signed-off-by: Jens Wiklander <jens.wiklan...@linaro.org>
---
 drivers/tee/tee_core.c    | 31 +++++++++++++++++++++++++++++++
 drivers/tee/tee_private.h |  2 ++
 drivers/tee/tee_rstmem.c  |  8 ++++++++
 include/uapi/linux/tee.h  | 27 +++++++++++++++++++++++++++
 4 files changed, 68 insertions(+)

diff --git a/drivers/tee/tee_core.c b/drivers/tee/tee_core.c
index f4a45b77753b..01a2a9513578 100644
--- a/drivers/tee/tee_core.c
+++ b/drivers/tee/tee_core.c
@@ -848,6 +848,35 @@ tee_ioctl_rstmem_alloc(struct tee_context *ctx,
        return fd;
 }
 
+static int
+tee_ioctl_rstmem_fd_info(struct tee_context *ctx,
+                        struct tee_ioctl_rstmem_fd_info __user *udata)
+{
+       struct tee_ioctl_rstmem_fd_info data;
+       struct dma_buf *dmabuf;
+       struct tee_shm *shm;
+
+       if (copy_from_user(&data, udata, sizeof(data)))
+               return -EFAULT;
+
+       dmabuf = dma_buf_get(data.fd);
+       if (IS_ERR(dmabuf))
+               return PTR_ERR(dmabuf);
+       shm = tee_rstmem_dmabuf_to_shm(ctx, dmabuf);
+       if (!IS_ERR(shm)) {
+               data.flags = 0;
+               data.use_case = shm->use_case;
+               data.id = shm->id;
+               data.size = shm->size;
+       }
+       dma_buf_put(dmabuf);
+       if (IS_ERR(shm))
+               return PTR_ERR(shm);
+       if (copy_to_user(udata, &data, sizeof(data)))
+               return -EFAULT;
+       return 0;
+}
+
 static long tee_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 {
        struct tee_context *ctx = filp->private_data;
@@ -874,6 +903,8 @@ static long tee_ioctl(struct file *filp, unsigned int cmd, 
unsigned long arg)
                return tee_ioctl_supp_send(ctx, uarg);
        case TEE_IOC_RSTMEM_ALLOC:
                return tee_ioctl_rstmem_alloc(ctx, uarg);
+       case TEE_IOC_RSTMEM_FD_INFO:
+               return tee_ioctl_rstmem_fd_info(ctx, uarg);
        default:
                return -EINVAL;
        }
diff --git a/drivers/tee/tee_private.h b/drivers/tee/tee_private.h
index bf97796909c0..b076089b2512 100644
--- a/drivers/tee/tee_private.h
+++ b/drivers/tee/tee_private.h
@@ -25,5 +25,7 @@ struct tee_shm *tee_shm_register_user_buf(struct tee_context 
*ctx,
                                          unsigned long addr, size_t length);
 struct dma_buf *tee_rstmem_alloc(struct tee_context *ctx, u32 flags,
                                 u32 use_case, size_t size, int *shm_id);
+struct tee_shm *tee_rstmem_dmabuf_to_shm(struct tee_context *ctx,
+                                        struct dma_buf *dmabuf);
 
 #endif /*TEE_PRIVATE_H*/
diff --git a/drivers/tee/tee_rstmem.c b/drivers/tee/tee_rstmem.c
index 3b27594ec30b..5108772f3ca0 100644
--- a/drivers/tee/tee_rstmem.c
+++ b/drivers/tee/tee_rstmem.c
@@ -178,3 +178,11 @@ struct dma_buf *tee_rstmem_alloc(struct tee_context *ctx, 
u32 flags,
        tee_device_put(teedev);
        return dmabuf;
 }
+
+struct tee_shm *tee_rstmem_dmabuf_to_shm(struct tee_context *ctx,
+                                        struct dma_buf *dmabuf)
+{
+       if (dmabuf->ops != &rstmem_generic_buf_ops)
+               return ERR_PTR(-EINVAL);
+       return dmabuf->priv;
+}
diff --git a/include/uapi/linux/tee.h b/include/uapi/linux/tee.h
index 88834448debb..30ab5bd80a55 100644
--- a/include/uapi/linux/tee.h
+++ b/include/uapi/linux/tee.h
@@ -431,6 +431,33 @@ struct tee_ioctl_rstmem_alloc_data {
 #define TEE_IOC_RSTMEM_ALLOC     _IOWR(TEE_IOC_MAGIC, TEE_IOC_BASE + 10, \
                                       struct tee_ioctl_rstmem_alloc_data)
 
+/**
+ * struct tee_ioctl_rstmem_fd_info - Restricted memory information
+ * @fd:                [in] File descriptor returned from the previous 
allocation
+ * @flags:     [out] Flags from the allocation
+ * @use_case:  [out] Restricted memory use case, TEE_IOC_UC_*
+ * @id:                [out] Identifier of the restricted memory
+ * @size:      [out] Size of the restricted memory
+ */
+struct tee_ioctl_rstmem_fd_info {
+       __s32 fd;
+       __u32 flags;
+       __u32 use_case;
+       __s32 id;
+       __u64 size;
+};
+
+/**
+ * TEE_IOC_RSTMEM_FD_INFO - get restricted memory information from an fd
+ *
+ * Returns information about a previously allocated restricted memory
+ * dma-buf file descriptor.
+ *
+ * Returns 0 on success or < 0 on failure
+ */
+#define TEE_IOC_RSTMEM_FD_INFO _IOWR(TEE_IOC_MAGIC, TEE_IOC_BASE + 11, \
+                                     struct tee_ioctl_rstmem_fd_info)
+
 /*
  * Five syscalls are used when communicating with the TEE driver.
  * open(): opens the device associated with the driver
-- 
2.43.0

Reply via email to