tee: optee: Move pool_op helper functions
authorBalint Dobszay <balint.dobszay@arm.com>
Mon, 25 Mar 2024 15:11:02 +0000 (16:11 +0100)
committerJens Wiklander <jens.wiklander@linaro.org>
Wed, 3 Apr 2024 11:58:20 +0000 (13:58 +0200)
Move the pool alloc and free helper functions from the OP-TEE driver to
the TEE subsystem, since these could be reused in other TEE drivers.
This patch is not supposed to change behavior, it's only reorganizing
the code.

Reviewed-by: Sumit Garg <sumit.garg@linaro.org>
Suggested-by: Jens Wiklander <jens.wiklander@linaro.org>
Signed-off-by: Balint Dobszay <balint.dobszay@arm.com>
Signed-off-by: Jens Wiklander <jens.wiklander@linaro.org>
drivers/tee/optee/core.c
drivers/tee/optee/ffa_abi.c
drivers/tee/optee/optee_private.h
drivers/tee/optee/smc_abi.c
drivers/tee/tee_shm.c
include/linux/tee_core.h

index f762e3a2511928d5fac8150dd01ddfbe53c3754b..39e688d4e974bc09e35a714800475cea41101bfa 100644 (file)
@@ -9,7 +9,6 @@
 #include <linux/crash_dump.h>
 #include <linux/errno.h>
 #include <linux/io.h>
-#include <linux/mm.h>
 #include <linux/module.h>
 #include <linux/slab.h>
 #include <linux/string.h>
 #include <linux/types.h>
 #include "optee_private.h"
 
-int optee_pool_op_alloc_helper(struct tee_shm_pool *pool, struct tee_shm *shm,
-                              size_t size, size_t align,
-                              int (*shm_register)(struct tee_context *ctx,
-                                                  struct tee_shm *shm,
-                                                  struct page **pages,
-                                                  size_t num_pages,
-                                                  unsigned long start))
-{
-       size_t nr_pages = roundup(size, PAGE_SIZE) / PAGE_SIZE;
-       struct page **pages;
-       unsigned int i;
-       int rc = 0;
-
-       /*
-        * Ignore alignment since this is already going to be page aligned
-        * and there's no need for any larger alignment.
-        */
-       shm->kaddr = alloc_pages_exact(nr_pages * PAGE_SIZE,
-                                      GFP_KERNEL | __GFP_ZERO);
-       if (!shm->kaddr)
-               return -ENOMEM;
-
-       shm->paddr = virt_to_phys(shm->kaddr);
-       shm->size = nr_pages * PAGE_SIZE;
-
-       pages = kcalloc(nr_pages, sizeof(*pages), GFP_KERNEL);
-       if (!pages) {
-               rc = -ENOMEM;
-               goto err;
-       }
-
-       for (i = 0; i < nr_pages; i++)
-               pages[i] = virt_to_page((u8 *)shm->kaddr + i * PAGE_SIZE);
-
-       shm->pages = pages;
-       shm->num_pages = nr_pages;
-
-       if (shm_register) {
-               rc = shm_register(shm->ctx, shm, pages, nr_pages,
-                                 (unsigned long)shm->kaddr);
-               if (rc)
-                       goto err;
-       }
-
-       return 0;
-err:
-       free_pages_exact(shm->kaddr, shm->size);
-       shm->kaddr = NULL;
-       return rc;
-}
-
-void optee_pool_op_free_helper(struct tee_shm_pool *pool, struct tee_shm *shm,
-                              int (*shm_unregister)(struct tee_context *ctx,
-                                                    struct tee_shm *shm))
-{
-       if (shm_unregister)
-               shm_unregister(shm->ctx, shm);
-       free_pages_exact(shm->kaddr, shm->size);
-       shm->kaddr = NULL;
-       kfree(shm->pages);
-       shm->pages = NULL;
-}
-
 static void optee_bus_scan(struct work_struct *work)
 {
        WARN_ON(optee_enumerate_devices(PTA_CMD_GET_DEVICES_SUPP));
index cee8ccb84cb8c6d20892cd1f0bca167627972459..3235e1c719e84b75c591649453e02f3177ae9e81 100644 (file)
@@ -374,14 +374,14 @@ static int optee_ffa_shm_unregister_supp(struct tee_context *ctx,
 static int pool_ffa_op_alloc(struct tee_shm_pool *pool,
                             struct tee_shm *shm, size_t size, size_t align)
 {
-       return optee_pool_op_alloc_helper(pool, shm, size, align,
-                                         optee_ffa_shm_register);
+       return tee_dyn_shm_alloc_helper(shm, size, align,
+                                       optee_ffa_shm_register);
 }
 
 static void pool_ffa_op_free(struct tee_shm_pool *pool,
                             struct tee_shm *shm)
 {
-       optee_pool_op_free_helper(pool, shm, optee_ffa_shm_unregister);
+       tee_dyn_shm_free_helper(shm, optee_ffa_shm_unregister);
 }
 
 static void pool_ffa_op_destroy_pool(struct tee_shm_pool *pool)
index a0698ac18993ae02127b792f22c4fee9528098a7..429cc20be5ccf05afb6dbe061fff729d00ca1c50 100644 (file)
@@ -283,18 +283,6 @@ int optee_cancel_req(struct tee_context *ctx, u32 cancel_id, u32 session);
 int optee_enumerate_devices(u32 func);
 void optee_unregister_devices(void);
 
-int optee_pool_op_alloc_helper(struct tee_shm_pool *pool, struct tee_shm *shm,
-                              size_t size, size_t align,
-                              int (*shm_register)(struct tee_context *ctx,
-                                                  struct tee_shm *shm,
-                                                  struct page **pages,
-                                                  size_t num_pages,
-                                                  unsigned long start));
-void optee_pool_op_free_helper(struct tee_shm_pool *pool, struct tee_shm *shm,
-                              int (*shm_unregister)(struct tee_context *ctx,
-                                                    struct tee_shm *shm));
-
-
 void optee_remove_common(struct optee *optee);
 int optee_open(struct tee_context *ctx, bool cap_memref_null);
 void optee_release(struct tee_context *ctx);
index 9c296b887dc11acdaf344702d6db50f299c5a7c4..734c484ed0f6862c32cbde9a1d7644b543a88217 100644 (file)
@@ -592,19 +592,18 @@ static int pool_op_alloc(struct tee_shm_pool *pool,
         * to be registered with OP-TEE.
         */
        if (shm->flags & TEE_SHM_PRIV)
-               return optee_pool_op_alloc_helper(pool, shm, size, align, NULL);
+               return tee_dyn_shm_alloc_helper(shm, size, align, NULL);
 
-       return optee_pool_op_alloc_helper(pool, shm, size, align,
-                                         optee_shm_register);
+       return tee_dyn_shm_alloc_helper(shm, size, align, optee_shm_register);
 }
 
 static void pool_op_free(struct tee_shm_pool *pool,
                         struct tee_shm *shm)
 {
        if (!(shm->flags & TEE_SHM_PRIV))
-               optee_pool_op_free_helper(pool, shm, optee_shm_unregister);
+               tee_dyn_shm_free_helper(shm, optee_shm_unregister);
        else
-               optee_pool_op_free_helper(pool, shm, NULL);
+               tee_dyn_shm_free_helper(shm, NULL);
 }
 
 static void pool_op_destroy_pool(struct tee_shm_pool *pool)
index 96a45c81742784035a92805a1a7365b603518fc4..daf6e5cfd59ae2a5f98d56113d4c3e23ca175256 100644 (file)
@@ -5,6 +5,7 @@
 #include <linux/anon_inodes.h>
 #include <linux/device.h>
 #include <linux/idr.h>
+#include <linux/io.h>
 #include <linux/mm.h>
 #include <linux/sched.h>
 #include <linux/slab.h>
@@ -202,6 +203,70 @@ struct tee_shm *tee_shm_alloc_priv_buf(struct tee_context *ctx, size_t size)
 }
 EXPORT_SYMBOL_GPL(tee_shm_alloc_priv_buf);
 
+int tee_dyn_shm_alloc_helper(struct tee_shm *shm, size_t size, size_t align,
+                            int (*shm_register)(struct tee_context *ctx,
+                                                struct tee_shm *shm,
+                                                struct page **pages,
+                                                size_t num_pages,
+                                                unsigned long start))
+{
+       size_t nr_pages = roundup(size, PAGE_SIZE) / PAGE_SIZE;
+       struct page **pages;
+       unsigned int i;
+       int rc = 0;
+
+       /*
+        * Ignore alignment since this is already going to be page aligned
+        * and there's no need for any larger alignment.
+        */
+       shm->kaddr = alloc_pages_exact(nr_pages * PAGE_SIZE,
+                                      GFP_KERNEL | __GFP_ZERO);
+       if (!shm->kaddr)
+               return -ENOMEM;
+
+       shm->paddr = virt_to_phys(shm->kaddr);
+       shm->size = nr_pages * PAGE_SIZE;
+
+       pages = kcalloc(nr_pages, sizeof(*pages), GFP_KERNEL);
+       if (!pages) {
+               rc = -ENOMEM;
+               goto err;
+       }
+
+       for (i = 0; i < nr_pages; i++)
+               pages[i] = virt_to_page((u8 *)shm->kaddr + i * PAGE_SIZE);
+
+       shm->pages = pages;
+       shm->num_pages = nr_pages;
+
+       if (shm_register) {
+               rc = shm_register(shm->ctx, shm, pages, nr_pages,
+                                 (unsigned long)shm->kaddr);
+               if (rc)
+                       goto err;
+       }
+
+       return 0;
+err:
+       free_pages_exact(shm->kaddr, shm->size);
+       shm->kaddr = NULL;
+       return rc;
+}
+EXPORT_SYMBOL_GPL(tee_dyn_shm_alloc_helper);
+
+void tee_dyn_shm_free_helper(struct tee_shm *shm,
+                            int (*shm_unregister)(struct tee_context *ctx,
+                                                  struct tee_shm *shm))
+{
+       if (shm_unregister)
+               shm_unregister(shm->ctx, shm);
+       free_pages_exact(shm->kaddr, shm->size);
+       shm->kaddr = NULL;
+       kfree(shm->pages);
+       shm->pages = NULL;
+}
+EXPORT_SYMBOL_GPL(tee_dyn_shm_free_helper);
+
 static struct tee_shm *
 register_shm_helper(struct tee_context *ctx, struct iov_iter *iter, u32 flags,
                    int id)
index d9b3ba8e8fa9ce52df420d028711ca41591ea6ad..efd16ed52315828edb7735978c0677d199485910 100644 (file)
@@ -232,6 +232,16 @@ void *tee_get_drvdata(struct tee_device *teedev);
  */
 struct tee_shm *tee_shm_alloc_priv_buf(struct tee_context *ctx, size_t size);
 
+int tee_dyn_shm_alloc_helper(struct tee_shm *shm, size_t size, size_t align,
+                            int (*shm_register)(struct tee_context *ctx,
+                                                struct tee_shm *shm,
+                                                struct page **pages,
+                                                size_t num_pages,
+                                                unsigned long start));
+void tee_dyn_shm_free_helper(struct tee_shm *shm,
+                            int (*shm_unregister)(struct tee_context *ctx,
+                                                  struct tee_shm *shm));
+
 /**
  * tee_shm_is_dynamic() - Check if shared memory object is of the dynamic kind
  * @shm:       Shared memory handle